﻿using MediatR;
using System;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.BatchManagements
{
    public class BuclkWeightListHander : IRequestHandler<BuclkWeightListCommand, Resqults<BuclkWeight>>
    {
        private readonly IBateRepository<WhiteNotes> whiteNotes;
        private readonly IBateRepository<BulkSlaughter> bulk;

        public BuclkWeightListHander(IBateRepository<WhiteNotes> whiteNotes, IBateRepository<BulkSlaughter> bulk)
        {
            this.whiteNotes = whiteNotes;
            this.bulk = bulk;
        }

        public async Task<Resqults<BuclkWeight>> Handle(BuclkWeightListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<BuclkWeight> res = new Resqults<BuclkWeight>();
                var list = (from a in bulk.GetAll()
                           select new BuclkWeight()
                           {
                               SlaughterNumber=a.BulkSlaughterNo,
                               BulkSlaughterNum=a.BulkSlaughterNum,
                               WeightNum=0,
                               Num= a.BulkSlaughterNum
                           }).ToList();
               
                foreach (var item in list)
                {
                    var whietList = whiteNotes.GetAll().Where(x=>x.SlaughterNumber == item.SlaughterNumber).FirstOrDefault();
                    if (whietList != null)
                    {
                        item.WeightNum = whietList.WhiteNoteNum;
                        item.Num -= item.WeightNum;
                    }
                }
                res.Date = list;
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
